﻿package mortal.game.scene3D.display3d.text3d.dynamicText3d
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.core.*;
    import frEngine.event.*;
    import frEngine.render.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.registType.*;
    import mortal.game.scene3D.*;

    public class Text3DRender extends DefaultRender
    {
        private var _textSurface:FrSurface3D;
        private var _textMaterial:ShaderBase;
        private var _textPosListRegister:VcParam;
        private var _textTextureRegister:FsParam;
        private var _curIndex:int = 0;
        private var _textConst:Vector.<Number>;
        private var _scene:Scene3D;
        private static var _instance:Text3DRender;

        public function Text3DRender()
        {
            if (_instance && _instance != this)
            {
                return;
            }
            _instance = this;
            return;
        }// end function

        public function init(param1:GameScene3D) : void
        {
            this.inittexts();
            this.initMaterial(param1);
            return;
        }// end function

        public function reinit(param1:GameScene3D) : void
        {
            return;
        }// end function

        private function initMaterial(param1:GameScene3D) : void
        {
            this._scene = param1;
            var _loc_2:* = Global3D.nullBitmapDataTexture;
            this._textMaterial = new ShaderBase("Text3DRender", new Text3DVertexFilter(), new Text3DFragmentFilter(_loc_2));
            this._textMaterial.materialParams.uvRepeat = false;
            this._textMaterial.materialParams.setBlendMode(EBlendType.BLEND_ALPHA0);
            this._textMaterial.materialParams.depthWrite = false;
            FrEventDispatcher.instance.proxyAddEventListener(this._textMaterial, Engine3dEventName.MATERIAL_REBUILDER, this.reBuildHander);
            this._textMaterial.buildBytes();
            return;
        }// end function

        private function reBuildHander(event:Event) : void
        {
            this._textPosListRegister = this._textMaterial.getParam("{textPosList}", true);
            this._textTextureRegister = this._textMaterial.getParam("{textBg0}", false);
            this._textConst = this._textMaterial.getParam("{textConst}", true).value;
            FrEventDispatcher.instance.proxyRemoveEventListener(this._textMaterial, Engine3dEventName.MATERIAL_REBUILDER, this.reBuildHander);
            return;
        }// end function

        private function inittexts() : void
        {
            var _loc_8:int = 0;
            this._textSurface = new FrSurface3D("texts");
            var _loc_1:* = this._textSurface.addVertexData(FilterName_ID.POSITION_ID, 3, true, null);
            this._textSurface.addVertexDataToTargetBuffer(FilterName_ID.UV_ID, 2, null, _loc_1);
            var _loc_2:* = this.createSinglePlaneVector(1, 1);
            var _loc_3:* = new Vector.<Number>;
            var _loc_4:* = this._textSurface.addVertexData(FilterName_ID.SKIN_INDICES_ID, 1, true, null);
            var _loc_5:* = _loc_4.vertexVector;
            var _loc_6:int = 0;
            while (_loc_6 < 100)
            {
                
                _loc_3 = _loc_3.concat(_loc_2);
                _loc_5.push(_loc_6, _loc_6, _loc_6, _loc_6);
                _loc_6++;
            }
            _loc_1.vertexVector = _loc_3;
            var _loc_7:* = new Vector.<uint>;
            _loc_6 = 0;
            while (_loc_6 < 100)
            {
                
                _loc_8 = _loc_6 * 4;
                _loc_7.push(_loc_8, _loc_8 + 2, (_loc_8 + 1));
                _loc_7.push(_loc_8 + 3, _loc_8 + 2, _loc_8);
                _loc_6++;
            }
            this._textSurface.indexVector = _loc_7;
            return;
        }// end function

        private function createSinglePlaneVector(param1:Number, param2:Number) : Vector.<Number>
        {
            var _loc_6:Vector3D = null;
            var _loc_3:* = new Vector.<Vector3D>;
            _loc_3.push(new Vector3D(0, 0, 0));
            _loc_3.push(new Vector3D(param1, 0, 0));
            _loc_3.push(new Vector3D(param1, param2, 0));
            _loc_3.push(new Vector3D(0, param2, 0));
            var _loc_4:* = new Vector.<Point>;
            _loc_4.push(new Point(0, 1));
            _loc_4.push(new Point(1, 1));
            _loc_4.push(new Point(1, 0));
            _loc_4.push(new Point(0, 0));
            var _loc_5:* = new Vector.<Number>;
            var _loc_7:int = 0;
            while (_loc_7 < 4)
            {
                
                _loc_6 = _loc_3[_loc_7];
                _loc_5.push(_loc_6.x, _loc_6.y, _loc_6.z, _loc_4[_loc_7].x, _loc_4[_loc_7].y);
                _loc_7++;
            }
            return _loc_5;
        }// end function

        override public function draw(param1:Mesh3D, param2:ShaderBase = null) : void
        {
            var _loc_8:Vector.<Number> = null;
            if (!this._textPosListRegister)
            {
                return;
            }
            var _loc_9:String = this;
            var _loc_10:* = this._curIndex + 1;
            _loc_9._curIndex = _loc_10;
            if (this._curIndex > 4)
            {
                this._curIndex = 0;
                Text3DFactory.instance.checkToUploadTexture();
            }
            if (!this._textMaterial.hasPrepared(param1, this._textSurface))
            {
                return;
            }
            var _loc_3:* = Text3DMesh(param1);
            var _loc_4:* = _loc_3.listArr;
            this._textConst[0] = this._scene.camera.zoom;
            this._textConst[3] = _loc_3.text3dWidth;
            var _loc_5:* = _loc_4.length;
            var _loc_6:* = this._textMaterial.materialParams;
            var _loc_7:int = 0;
            while (_loc_7 < _loc_5)
            {
                
                var _loc_9:* = Global3D;
                var _loc_10:* = Global3D.objectsDrawn + 1;
                _loc_9.objectsDrawn = _loc_10;
                _loc_8 = _loc_4[_loc_7];
                this._textPosListRegister.value = _loc_8;
                this._textTextureRegister.value = _loc_3.textBg0;
                this._textMaterial.draw(param1, this._textSurface, _loc_6.depthCompare, _loc_6.cullFace, false, _loc_6.sourceFactor, _loc_6.destFactor, 0, _loc_8.length * 0.5);
                _loc_7++;
            }
            return;
        }// end function

        public static function get instance() : Text3DRender
        {
            if (!_instance)
            {
                new Text3DRender;
            }
            return _instance;
        }// end function

    }
}
